/**
 * <p>Title: liteflow</p>
 * <p>Description: 轻量级的组件式流程框架</p>
 * @author Bryan.Zhang
 * @email weenyc31@163.com
 * @Date 2020/4/1
 */
package io.mft.flow.core.executable.condition;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.mft.commons.function.ExecuteReturn;
import io.mft.flow.core.executable.Condition;
import io.mft.flow.core.executable.ConditionExecutable;
import io.mft.flow.core.executable.ConditionType;
import io.mft.flow.core.executable.Executable;
import io.mft.flow.core.executable.ExecutableType;
import io.mft.flow.core.executable.drive.ConditionDriveSourceProvider;
import io.mft.flow.core.executable.drive.ConditionDriverType;
import io.mft.flow.data.ChainContext;
import io.mft.flow.data.ExecutableResponse;
/**
 * 接口编排异步逻辑单元
 * @说明：TODO
 * @类型名称：WhenCondition
 * @创建者: 胡常建  
 * @创建时间: 2021年3月22日 下午4:06:07
 * @修改者: 胡常建  
 * @修改时间: 2021年3月22日 下午4:06:07
 */
public class AsyncCondition extends Condition implements ConditionDriveSourceProvider{

	Logger logger=LoggerFactory.getLogger(getClass());
	ConditionExecutable lastConditionExecutable;

	public AsyncCondition(List<ConditionExecutable> nodeList) {
		super(ConditionDriverType.noneDeliverReturn.chainConditionDriver(),nodeList);
		List<ConditionExecutable> conditionExecutables = getNodeList();
		lastConditionExecutable = conditionExecutables.get(conditionExecutables.size() - 1);
	}

	public ConditionExecutable getLastConditionExecutable() {
		return lastConditionExecutable;
	}
	@Override
	public ConditionType conditionType() {
		return ConditionType.async;
	}
	@Override
	public ExecuteReturn execute(ChainContext context) throws Exception {
		ExecuteReturn executeReturn = ExecuteReturn.builder().build();
		for (ConditionExecutable conditionExecutable : getNodeList()) {
			ExecutableResponse response=new ExecutableResponse();
			String uniqueId = conditionExecutable.getUniqueId();
			ChainContext chainContext=new ChainContext(chainCondition.getChain().getChainId(),context.getRequest(),context.getRequest(), response,uniqueId);
			Executable executable=getFlowExecutor().getExecutable(ExecutableType.NODE,conditionExecutable.getExecutableId());
			executeReturn=executable.execute(chainContext);
		}
		if (chainCondition.getNextChainCondition() != null) {
			executeReturn=chainConditionDriver.drive(context,this);
		}
		return executeReturn;
	}

	@Override
	public ProvideResult provide(ChainContext context) {
		ProvideResult provideResult=new ProvideResult();
		provideResult.setList(context.getResponse().listObject());
		return provideResult;
	}
}
